import App from '@/App.vue'

const app = createApp(App)

/**
 * 参数处理
 *
 * @param {*} params  参数
 */
export function tansParams(params: { [x: string]: any }) {
	let result = "";
	for (const propName of Object.keys(params)) {
		const value = params[propName];
		var part = encodeURIComponent(propName) + "=";
		if (value !== null && value !== "" && typeof value !== "undefined") {
			if (typeof value === "object") {
				for (const key of Object.keys(value)) {
					// prettier-ignore
					if (value[key] !== null && value !== "" && typeof value[key] !== "undefined") {
                        let params = propName + "[" + key + "]";
                        var subPart = encodeURIComponent(params) + "=";
                        result += subPart + encodeURIComponent(value[key]) + "&";
                    }
				}
			} else {
				result += part + encodeURIComponent(value) + "&";
			}
		}
	}
	return result;
}

export function zconfirm(msg: string, callBackFun?: any) {
    uni.showModal({
      content: msg,
      success: function (res) {
        if (res.confirm) {
          callBackFun(true)
        } else if (res.cancel) {
          callBackFun(false)
        }
      },
    })
  }
  export function zalert(msg: string, callBackFun?: any) {
    uni.showModal({
      title: '提示',
      content: msg,
      showCancel: false,
      success: function () {
        if (callBackFun) {
          callBackFun()
        }
      },
    })
  }
  export function toast(msg: string) {
    uni.showToast({
      title: msg,
      duration: 2000,
      mask: false,
      icon: 'none',
    })
  }
  
  export const deepClone = function (origin, target: object = {}) {
    for (const prop in target) {
      if (target[prop] !== null && typeof target[prop] === 'object') {
        origin[prop] = Object.prototype.toString.call(target[prop]) === '[object Array]' ? [] : {}
        deepClone(origin[prop], target[prop])
      } else {
        origin[prop] = target[prop]
      }
    }
  }
  
  // 存储用户openId
export function setUserOpenId(openid = '1') {
    uni.setStorageSync('openId', openid)
    app.config.globalProperties.openId = openid

}

// 获取用户openId
export function getUserOpenId() {
    let openId = uni.getStorageSync('openId') || app.config.globalProperties.openId || ''
    return openId
}